package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.model.Charter;
import com.ricchieri.model.CharterExpense;
import com.ricchieri.model.CharterExpenseType;
import com.ricchieri.service.interf.ICharterExpenseService;
import com.ricchieri.service.interf.ICharterExpenseTypeService;
import com.ricchieri.util.FacesUtil;

public class CommonCharterExpenseManageBean extends GenericManageBean {

	private static final long serialVersionUID = 7987170449877555072L;
	
	@ManagedProperty(value = "#{CharterExpenseTypeService}")
	ICharterExpenseTypeService charterExpenseTypeService;
	
	@ManagedProperty(value = "#{CharterExpenseService}")
	ICharterExpenseService charterExpenseService;
	
	private Charter charter;
	private CharterExpense charterExpense;
	private String selectedCharterExpenseType;
	private Map<String,String> charterExpensivesTypes = null;

	@PostConstruct
	public void init() {
		charterExpense = (CharterExpense) FacesUtil.getParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE);
		charter = (Charter) FacesUtil.getParametersFlash(BusinessSettings.KEY_CHARTER);
		
		if(charter == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_CHARTER);
		}
		
	}
	
	public Charter getCharter() {
		return charter;
	}

	public void setCharter(Charter charter) {
		this.charter = charter;
	}
	
	public Map<String, String> getCharterExpensivesTypes() {
		if (charterExpensivesTypes == null){
			List<CharterExpenseType> charterT = getCharterExpenseTypeService().getAllCharterExpenseTypes();		
			
			charterExpensivesTypes = new HashMap<String, String>();
			
			for (CharterExpenseType charterType : charterT) {
				charterExpensivesTypes.put(FacesUtil.getResourceBundle().getString(charterType.getDescription()), String.valueOf(charterType.getId()));
			}
		}
		
		return charterExpensivesTypes;
	}
	
	public String getSelectedCharterExpenseType() {
		return selectedCharterExpenseType;
	}

	public void setSelectedCharterExpenseType(String selectedCharterExpenseType) {
		this.selectedCharterExpenseType = selectedCharterExpenseType;
	}
	
	public ICharterExpenseService getCharterExpenseService() {
		return charterExpenseService;
	}

	public void setCharterExpenseService(ICharterExpenseService charterService) {
		this.charterExpenseService = charterService;
	}

	public ICharterExpenseTypeService getCharterExpenseTypeService() {
		return charterExpenseTypeService;
	}

	public void setCharterExpenseTypeService(
			ICharterExpenseTypeService charterExpensiveTypeService) {
		this.charterExpenseTypeService = charterExpensiveTypeService;
	}

	public void setCharterExpensivesTypes(Map<String, String> charterExpensivesTypes) {
		this.charterExpensivesTypes = charterExpensivesTypes;
	}
	
	protected CharterExpenseType obtainCharterExpenseType(String id) {
		List<CharterExpenseType> charterT = getCharterExpenseTypeService().getAllCharterExpenseTypes();	
		for (CharterExpenseType charterType : charterT) {
			if(id.equals(String.valueOf(charterType.getId()))) {
				return charterType;
			}
		}
		return null;
	}

	public CharterExpense getCharterExpense() {
		if(charterExpense == null) {
			charterExpense = new CharterExpense();
		}
		return charterExpense;
	}

	public void setCharterExpense(CharterExpense charterExpense) {
		this.charterExpense = charterExpense;
	}
}
